package com.derbtec.refale.activities;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.derbtec.refale.R;
import com.derbtec.refale.transaction.GetEventParticipantsDetailAsyncTask;
import com.derbtec.refale.transaction.SetEventParticipantAcceptAsyncTask;
import com.derbtec.refale.transaction.SetEventParticipantDeclineAsyncTask;
import com.derbtec.refale.util.Session;

public class EventDetailViewActivity extends Activity {
	
	private String eventname, eventcreator,eventplace, eventdescription;
	private int eventid, eventcreatorid, participantscount;
	private Date eventlastedit, eventstart, eventend;
	private TextView tveventtitle, tveventcreator, tveventlastedit, tveventstart, tveventend, tveventplace, tveventparticipants, tveventressources, tveventdescription;
	private Button btParticipantReaction, btParticipantsDetail;
	private Boolean participantself = false, isAdmin = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_detail_view);
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yy kk:mm");
        
        Bundle extras = getIntent().getExtras();
        setEventname(extras.getString("eventname"));
        setEventcreator(extras.getString("eventcreator"));
        setEventplace(extras.getString("eventplace"));
        setEventdescription(extras.getString("eventdescription"));
        setEventid(extras.getInt("eventid"));
        setEventcreatorid(extras.getInt("eventcreatorid"));
        setParticipantscount(extras.getInt("participantscount"));
        setParticipantself(extras.getBoolean("participantself"));
        isAdmin(extras.getBoolean("admin"));
        try
        {
        	setEventlastedit(sdf.parse(extras.getString("eventlastedit")));
        	setEventstart(sdf.parse(extras.getString("eventstart")));
        	setEventend(sdf.parse(extras.getString("eventend")));
		}
        catch (ParseException e)
        {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        tveventtitle = (TextView)findViewById(R.id.tvEventTitle);
        tveventcreator = (TextView)findViewById(R.id.tvEventCreator);
        tveventdescription = (TextView)findViewById(R.id.tvEventDescription);
        tveventend = (TextView)findViewById(R.id.tvEventEnddate);
        tveventlastedit = (TextView)findViewById(R.id.tvEventLastedit);
        tveventparticipants = (TextView)findViewById(R.id.tvEventParticipants);
        tveventplace = (TextView)findViewById(R.id.tvEventPlace);
        tveventressources = (TextView)findViewById(R.id.tvEventRessources);
        tveventstart = (TextView)findViewById(R.id.tvEventStartdate);
        btParticipantReaction = (Button)findViewById(R.id.btParticipantReaction);
        btParticipantsDetail = (Button)findViewById(R.id.btEventShowParticipants);
        
        tveventtitle.setText(eventname);
        tveventcreator.setText(eventcreator);
        tveventdescription.setText(eventdescription);
        tveventend.setText(df.format(eventend));
        tveventlastedit.setText(df.format(eventlastedit));
        tveventplace.setText(eventplace);
        tveventressources.setText("");
        tveventstart.setText(df.format(eventstart));
        tveventparticipants.setText(String.valueOf(participantscount));
        
        if(participantself)
        	btParticipantReaction.setText("Decline");
        else
        	btParticipantReaction.setText("Accept");
        
        if(Session.getUserid() == eventcreatorid)
        	tveventcreator.setTextColor(Color.parseColor("#00BFFF"));
        else if(isAdmin)
        	tveventcreator.setTextColor(Color.parseColor("#FF0000"));
        else
        	tveventcreator.setTextColor(Color.parseColor("#FFFFFF"));
        
        btParticipantReaction.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(participantself)
				{
	        		SetEventParticipantDeclineAsyncTask declineasynctask = new SetEventParticipantDeclineAsyncTask(EventDetailViewActivity.this, eventid);
					declineasynctask.execute();
				}
				else
				{
	        		SetEventParticipantAcceptAsyncTask acceptasynctask = new SetEventParticipantAcceptAsyncTask(EventDetailViewActivity.this, eventid);
					acceptasynctask.execute();					
				}
				
			}
		});
        
        btParticipantsDetail.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
        		GetEventParticipantsDetailAsyncTask participantsdetailasynctask = new GetEventParticipantsDetailAsyncTask(EventDetailViewActivity.this, eventid);
				participantsdetailasynctask.execute();
				
			}
		});
        
        

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_event_detail_view, menu);
        return true;
    }

	public String getEventname() {
		return eventname;
	}

	public void setEventname(String eventname) {
		this.eventname = eventname;
	}

	public String getEventcreator() {
		return eventcreator;
	}

	public void setEventcreator(String eventcreator) {
		this.eventcreator = eventcreator;
	}

	public String getEventplace() {
		return eventplace;
	}

	public void setEventplace(String eventplace) {
		this.eventplace = eventplace;
	}

	public String getEventdescription() {
		return eventdescription;
	}

	public void setEventdescription(String eventdescription) {
		this.eventdescription = eventdescription;
	}

	public int getEventid() {
		return eventid;
	}

	public void setEventid(int eventid) {
		this.eventid = eventid;
	}

	public int getEventcreatorid() {
		return eventcreatorid;
	}

	public void setEventcreatorid(int eventcreatorid) {
		this.eventcreatorid = eventcreatorid;
	}

	public Date getEventlastedit() {
		return eventlastedit;
	}

	public void setEventlastedit(Date eventlastedit) {
		this.eventlastedit = eventlastedit;
	}

	public Date getEventstart() {
		return eventstart;
	}

	public void setEventstart(Date eventstart) {
		this.eventstart = eventstart;
	}

	public Date getEventend() {
		return eventend;
	}

	public void setEventend(Date eventend) {
		this.eventend = eventend;
	}



	public int getParticipantscount() {
		return participantscount;
	}

	public void setParticipantscount(int participantscount) {
		this.participantscount = participantscount;
	}
	
	public void reloadEventParticipants(int participantscount, boolean participantself)
	{
		this.participantscount = participantscount;
		this.participantself = participantself;
		
		tveventparticipants.setText(String.valueOf(participantscount));
		if(participantself)
			btParticipantReaction.setText("Decline");
		else
			btParticipantReaction.setText("Accept");
	}

	public Boolean getParticipantself() {
		return participantself;
	}

	public void setParticipantself(Boolean participantself) {
		this.participantself = participantself;
	}

	public void startParticipantsDetailViewActivity(String jsonstring) {
		
		Intent eventDetail = new Intent(EventDetailViewActivity.this, EventParticipantsListViewActivity.class);
		eventDetail.putExtra("jsonstring", jsonstring);


		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
        parentActivity.startChildActivity("EventParticipantsDetailActivity", eventDetail);
		
	}

	public Boolean isAdmin() {
		return isAdmin;
	}

	public void isAdmin(Boolean admin) {
		this.isAdmin = admin;
	}
}
